// eq_tool.js
import {
    EQUtil
} from './eq_util.js';
/**
 * 通道频率信号
 */
class CHSign {
    constructor(Q, gain, freq) {
        this.Q = Q;           // double
        this.gain = gain;     // double
        this.freq = freq;     // int
    }
}

/**
 * 通道高低通过滤
 */
class CHPass {
    constructor(freq, oct_type, isHigh, isByPass) {
        this.freq = freq;             // int
        this.oct_type = oct_type;     // int
        this.isHigh = isHigh;         // bool
        this.isByPass = isByPass;     // bool
    }
}

/**
 * EQ 工具类
 */
class EqTool {
    /**
     * 叠加 eq、高通、低通，返回用于波形显示的列表
     * @param {CHSign[]} signList - 通道有多少个频点信号
     * @param {CHPass} highPass - 高通
     * @param {CHPass} lowPass - 低通
     * @param {{width: number, height: number}} size - 通道的频点组成的波形，显示的宽度
     * @param {number} signBy - EQ 高度缩放
     * @param {number} passBy - 高低通缩放
     * @returns {number[]} 映射后的整数
     */
    static eqTotal(signList, highPass, lowPass, size, signBy, passBy) {
        const width = Math.floor(size.width);
        let result = new Array(width).fill(0.0);

        // 1️⃣ 计算通道信号增益
        for (const sign of signList) {
            let list = new Array(width).fill(0.0);
            EQUtil.eq(sign.Q, sign.gain, sign.freq, list);
            result = result.map((v, i) => v + list[i] * signBy);
        }

        // 2️⃣ 计算高通
        let temp1 = new Array(width).fill(0.0);
        temp1 = EQUtil.filter(highPass.freq, highPass.oct_type, highPass.isHigh, highPass.isByPass, temp1);
        result = result.map((v, i) => v + temp1[i] * passBy);

        // 3️⃣ 计算低通
        let temp2 = new Array(width).fill(0.0);
        temp2 = EQUtil.filter(lowPass.freq, lowPass.oct_type, lowPass.isHigh, lowPass.isByPass, temp2);
        result = result.map((v, i) => v + temp2[i] * passBy);

        // 可选：进行高度缩放
        // result = result.map(v => v * 13);

        return result;
    }
}

export {
    CHPass, CHSign, EqTool
};

